HackerRank Red John is Back
https://www.hackerrank.com/challenges/red-john-is-back/problem
code: python
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'redJohn' function below.
#
# The function is expected to return an INTEGER.
# The function accepts INTEGER n as parameter.
#
def redJohn(n):
# Write your code here
# 4
res = n % 4
MAX_N = 10 ** 6
prime = None * MAX_N
is_prime = True * (MAX_N + 1)
def sieve(n):
p = 0
for i in range(n + 1):
is_primei = True
is_prime0 = is_prime1 = False
for i in range(2, n + 1):
if is_primei:
primep = i
p += 1
for j in range(2 * i, n + 1, i):
is_primej = False
return p
return sieve(res)
if __name__ == '__main__':
fptr = open(os.environ'OUTPUT_PATH', 'w')
t = int(input().strip())
for t_itr in range(t):
n = int(input().strip())
result = redJohn(n)
fptr.write(str(result) + '\n')
fptr.close()
解答
code: python
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'redJohn' function below.
#
# The function is expected to return an INTEGER.
# The function accepts INTEGER n as parameter.
#
def sito(n=300000):
is_prime = True * n
primes = []
for i in range(2, n):
if is_primei:
primes.append(i)
j = i+i
while j < n:
is_primej = False
j += i
return primes
PRIMES = sito()
# print(PRIMES) [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47...
def redJohn(n):
# Write your code here
res = 1, 1, 1, 1, 2
for i in range(5, n+1):
res.append(resi-1 + resi-4)
if resn <= 1:
return 0
i = 0
while i < len(PRIMES):
if PRIMESi > resn:
return i
i += 1
return int(round(resn / math.log(resn)))
if __name__ == '__main__':
fptr = open(os.environ'OUTPUT_PATH', 'w')
t = int(input().strip())
for t_itr in range(t):
n = int(input().strip())
result = redJohn(n)
fptr.write(str(result) + '\n')
fptr.close()
テーマ
#dp
メモ
https://www.youtube.com/watch?v=yxz2ng_YvBA
https://scrapbox.io/files/61d2c6b228002b0021321575.png